package com.wind.plugin.config;

import com.intellij.openapi.options.Configurable;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class CurrencyConfigurable implements Configurable {

    private final CurrencySettings currencySettings;
    private final JPanel settingsPanel;
    private final JList<String> currencyKeywordsList;
    private DefaultListModel<String> listModel;
    private final JTextField addKeywordField;
    private JButton addButton;
    private JButton removeButton;

    public CurrencyConfigurable() {
        currencySettings = CurrencySettings.getInstance();
        settingsPanel = new JPanel(new BorderLayout());

        // List to display current currency keywords
        List<String> keywords = currencySettings.getCurrencyKeywords();
        listModel = new DefaultListModel<>();
        for (String keyword : keywords) {
            listModel.addElement(keyword);
        }
        currencyKeywordsList = new JList<>(listModel);

        // Text field for adding new currency keyword
        addKeywordField = new JTextField(20);

        // Buttons for adding/removing keywords
        addButton = new JButton("Add");
        removeButton = new JButton("Remove");

        // Layout setup
        JPanel inputPanel = new JPanel(new FlowLayout());
        inputPanel.add(new JLabel("New Keyword:"));
        inputPanel.add(addKeywordField);
        inputPanel.add(addButton);
        inputPanel.add(removeButton);

        settingsPanel.add(new JScrollPane(currencyKeywordsList), BorderLayout.CENTER);
        settingsPanel.add(inputPanel, BorderLayout.SOUTH);

        // Add action listeners
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String keyword = addKeywordField.getText().trim();
                if (!keyword.isEmpty() && !listModel.contains(keyword)) {
                    currencySettings.addCurrencyKeyword(keyword);
                    listModel.addElement(keyword);
                    addKeywordField.setText(""); // Clear the input field
                }
            }
        });

        removeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedKeyword = currencyKeywordsList.getSelectedValue();
                if (selectedKeyword != null) {
                    currencySettings.removeCurrencyKeyword(selectedKeyword);
                    listModel.removeElement(selectedKeyword);
                }
            }
        });
    }

    @Override
    public @Nls(capitalization = Nls.Capitalization.Title) String getDisplayName() {
        return "Currency Settings";
    }

    @Override
    public @Nullable JComponent createComponent() {
        return settingsPanel;
    }

    @Override
    public boolean isModified() {
        List<String> currentKeywords = new ArrayList<>();
        for (int i = 0; i < listModel.getSize(); i++) {
            currentKeywords.add(listModel.getElementAt(i));
        }
        return !currentKeywords.equals(currencySettings.getCurrencyKeywords());
    }

    @Override
    public void apply() {
        // 保存用户修改的配置
        List<String> currentKeywords = new ArrayList<>();
        for (int i = 0; i < listModel.getSize(); i++) {
            currentKeywords.add(listModel.getElementAt(i));
        }
        currencySettings.setCurrencyKeywords(currentKeywords);
    }

    @Override
    public void reset() {
        // 恢复原始配置
        List<String> keywords = currencySettings.getCurrencyKeywords();
        listModel.clear();
        for (String keyword : keywords) {
            listModel.addElement(keyword);
        }
    }

    @Override
    public void disposeUIResources() {
        // 清理资源
    }
}
